<?php
/**
 * @package MWM Master Plugin
 * @version 0.5
 */
/*
Plugin Name: MWM Master Plugin
Description: A plugin to manage the implementation of various features across MWM websites
Author: Dustin Moore
Version: 0.5
Author URI: http://invexi.com
Slug: mwm-master-plugin
*/

function init($config) {

	// Set the config vars
	if(is_array($config)) {
//		die('init');
	} else {
//		die('init2');
//		return false;
	}
}

// Now we set that function up to execute when the init action is called
add_action( 'init', 'init' );

function set_footer_content($config) {
	
	extract( shortcode_atts( array(
		'style' => 'hidden',
		'text' => '',
	), $config ) );

	// Make sure the config is set
	if(is_array($config)) {
		
		// Process the attributes
		// Call display function
		switch ($config['style']) {
			case 'text':
				$content = set_content($config);
			break;
			case 'image-dark':
				$content = set_content($config);
			break;
			case 'image-white':
				$content = set_content($config);
			break;			
			case 'hidden':
			default:
			break;
		}
		
		// Print it
		echo $content;
		echo '';
	} else {
	// Run the default setup
		return false;
	}
	
}

add_action( 'get_footer', 'set_footer_content' );
add_shortcode( 'mwm_footer', 'set_footer_content' );
$footer_config = array(
		'style' => 'hidden',
		'text' => 'Morris Web Marketing',
		'link' => 'http://www.morriswebmarketing.com',
		'divstyle' => '',
		'pstyle' => '',
		'imgstyle' => '',
		'astyle' => 'color: #00f',
	);

function set_content($config) {
	
	// Import the default config	
	global $footer_config;
	
	// Replace config settings with that from the shortcode
	foreach($config as $type => $value) {
		if(!empty($config[$type]) || !is_null($config[$type])) {
			$footer_config[$type] = $value;
		}	
	}
	
	// -- Obscuring the link and text
	// Example obsursion
	// <script type='text/javascript'>var a = new Array('om','n.c','mai','@do','any','.em','ail');document.write("<a href='mailto:"+a[4]+a[5]+a[6]+a[3]+a[2]+a[1]+a[0]+"'>"+a[4]+a[5]+a[6]+a[3]+a[2]+a[1]+a[0]+"</a>");</script>
	$link_array = str_split($footer_config['link'],rand(2,4));
	
	// Set some counters to be used later
	$rand = rand(2,5);
	$count = $rand;
	
	// Push some elements off the front of the array and onto the end
	for($rand;$rand != 0;$rand--) {
		$element = array_shift($link_array);
		array_push($link_array,$element);
	}
	
	// Will be the the JS array
	$link_js = '';
	$key_count = count($link_array);
	
	// Loop through the array and build the string
	for($i = 0;$i <= $key_count;$i++) {
		$link_js .= '\'';
		$link_js .= $link_array[$i];
		$link_js .= '\',';
	}
	
	// Chop some junk off the end of the string
	$link_js = str_replace("','',", "'",$link_js);
	
	// Will be what the JS calls
	$link_js_call = '';
	
	// Loop through the array and build the string - this makes sure you start in the right spot
	for($i = $count;$i != 0;$i--) {
		$link_js_call .= '+a[';
		$link_js_call .= $key_count - $i;
		$link_js_call .= ']';
	}	
	
	// Finishes the loop above
	for($i = 0;$i <= ($key_count - $count - 1);$i++) {
		$link_js_call .= '+a[';
		$link_js_call .= $i;
		$link_js_call .= ']';
	}
	
	$text_array = str_split($footer_config['text'],rand(2,4));
	
	// Set some counters to be used later
	$rand = rand(2,5);
	$count = $rand;
	
	// Push some elements off the front of the array and onto the end
	for($rand;$rand != 0;$rand--) {
		$element = array_shift($text_array);
		array_push($text_array,$element);
	}
	
	// Will be the the JS array
	$text_js = '';
	$key_count = count($text_array);
	
	// Loop through the array and build the string
	for($i = 0;$i <= $key_count;$i++) {
		$text_js .= '\'';
		$text_js .= $text_array[$i];
		$text_js .= '\',';
	}
	
	// Chop some junk off the end of the string
	$text_js = str_replace("','',", "'",$text_js);
	
	// Will be what the JS calls
	$text_js_call = '';
	
	// Loop through the array and build the string - this makes sure you start in the right spot
	for($i = $count;$i != 0;$i--) {
		$text_js_call .= '+b[';
		$text_js_call .= $key_count - $i;
		$text_js_call .= ']';
	}	
	
	// Finishes the loop above
	for($i = 0;$i <= ($key_count - $count - 1);$i++) {
		$text_js_call .= '+b[';
		$text_js_call .= $i;
		$text_js_call .= ']';
	}
	
	switch ($footer_config['style']) {
		case 'text':
			$content = '
				<div class="footer_powered_by" style="'. $footer_config['divstyle'] .'">
					<p style="'. $footer_config['pstyle'] .'">Powered by 
					<script type=\'text/javascript\'>var a = new Array('.$link_js.');var b = new Array('.$text_js.');document.write("<a rel=\'nofollow\' href=\'"'.$link_js_call.'+"\'>"'.$text_js_call.'+"</a>");</script>
					</p>
				</div>
			';
			return $content;
		break;
		case 'image-dark':
			$content = '
				<div class="footer_powered_by" style="'. $footer_config['divstyle'] .'">
					<img src="/wp-content/plugins/mwm-master-plugin/images/logo-dark.png" style="'. $footer_config['imgstyle'] .'" />
				</div>
			';
			return $content;
		break;
		case 'image-white':
			$content = '
				<div class="footer_powered_by" style="'. $footer_config['divstyle'] .'">
					<img src="/wp-content/plugins/mwm-master-plugin/images/logo-white.png" style="'. $footer_config['imgstyle'] .'" />
				</div>
			';
			return $content;
		break;
		case 'hidden':
		default:
		break;
	}
}


// ---------------------------
// -- Custom Plugin Updater --
// ---------------------------

$this_file = __FILE__;
$update_check = 'http://www.mwm-0.com/wp_plugins/update.nfo';

// Exclude from WP updates
function mwm_updates_exclude( $r, $url ) {
	if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) )
		return $r; // Not a plugin update request. Bail immediately.
	$plugins = unserialize( $r['body']['plugins'] );
	unset( $plugins->plugins[ plugin_basename( __FILE__ ) ] );
	unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] );
	$r['body']['plugins'] = serialize( $plugins );
	return $r;
}

add_filter( 'http_request_args', 'mwm_updates_exclude', 5, 2 );

// Returns current plugin info.
function mwm_plugin_get($i) {
	global $this_file;
	if ( ! function_exists( 'get_plugins' ) )
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	$plugin_folder = get_plugins( '/' . plugin_basename( dirname( $this_file ) ) );
	$plugin_file = basename( ( $this_file ) );
	return $plugin_folder[$plugin_file][$i];
}

function mwm_check_update() {
	global $wp_version;
	global $this_file;
	global $update_check;
	$plugin_folder = plugin_basename( dirname( $this_file ) );
	$plugin_file = basename( ( $this_file ) );
	if ( defined( 'WP_INSTALLING' ) ) return false;

	$response = wp_remote_get( $update_check );
	list($version, $url) = explode('|', $response['body']);
	
	if(mwm_plugin_get("Version") == $version) return false;
	$plugin_transient = get_site_transient('update_plugins');
	$a = array(
		'slug' => $plugin_folder,
		'new_version' => $version,
		'url' => mwm_plugin_get("AuthorURI"),
		'package' => $url
	);
	$o = (object) $a;
	$plugin_transient->response[$plugin_folder.'/'.$plugin_file] = $o;
	set_site_transient('update_plugins', $plugin_transient);
}

add_action('init', 'mwm_check_update');

?>
